package com.acme.bankapp.domain.accounting;

import com.acme.bankapp.domain.Client;

abstract class AbstractAccount implements Account {
	
	public static final double DEFAULT_INITIAL_BALANCE = 0;


	protected double balance;
	
	private Client client;

	/**
	 * common constructor
	 * @param balance Initial balance
	 */
	public AbstractAccount(double balance) {
		if(balance < 0) throw new IllegalArgumentException("balance cannot be less then 0");
		this.balance = balance;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public double getBalance() {
		return balance;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void deposit(double x) {
		balance += x;

	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public abstract void withdraw(double x) throws NotEnoughFundsException;

	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public abstract double maximumAmountToWithdraw();
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setClient(Client client) {
		this.client = client;
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public Client getClient(){
		return client;
	}

	@Override
	public String toString() {
		return new StringBuilder().append(this.getClass().getSimpleName()).append(" : ")
		.append("balance: ").append(this.getBalance())
		.append(" max wihdraw: ")
		.append(this.maximumAmountToWithdraw()).append("\n").toString();
	}
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + getClass().getSimpleName().hashCode();
		result = prime * result + ((client == null) ? 0 : client.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (client == null) {
			if (other.client != null)
				return false;
		} else if (!client.equals(other.client))
			return false;
		if(!other.getClass().getSimpleName().equals(this.getClass().getSimpleName())){
			return false;
		}
		
		return true;
	}
	
	
}
